package com.itheima.health.service.impl;


import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class OrderSettingServicelmpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public List<OrderSettingParam> findPages(Integer year, Integer month) {
        log.info("预约信息查询");
        LocalDate begin = LocalDate.of(year, month, 1);
        LocalDate end = begin.withDayOfMonth(begin.lengthOfMonth());
        //根据年和月查询预约信息
        List<OrderSetting> orderSettingList = orderSettingDao.findPages(begin, end);

        //属性拷贝
        List<OrderSettingParam> orderSettingParamList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSettingParam orderSettingParam = new OrderSettingParam();
            BeanUtils.copyProperties(orderSetting, orderSettingParam);
            orderSettingParam.setDate(orderSetting.getOrderDate().getDate());
            orderSettingParamList.add(orderSettingParam);
        }
        return orderSettingParamList;
    }

    @Override
    public void add(OrderSetting orderSetting) {
        log.info("设置预约人数");
        OrderSetting newOrderSetting = orderSettingDao.getByDate(orderSetting);
        if (newOrderSetting != null){
            //如果存在则修改
            orderSettingDao.editByDate(orderSetting);
        }else {
            //如果不存在则新增该日期并设置可预约人数
            orderSettingDao.add(orderSetting);
        }
    }


    @Override
    public void batchAdd(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting os = orderSettingDao.getByDate(orderSetting);
            if (os == null){
                //执行新增操作
                orderSettingDao.add(orderSetting);

            }else {
                //执行修改操作
                orderSettingDao.editByDate(orderSetting);
            }
        }
    }
}






